package entities;

import javax.persistence.*;
import java.io.Serializable;

@Embeddable
public class FriendOfPK implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	 @ManyToOne
	 @JoinColumn(name="user1", referencedColumnName="userId", nullable=false)
	private RegisteredUser user1;
	 
	 @ManyToOne
	 @JoinColumn(name="user2", referencedColumnName="userId", nullable=false)
	private RegisteredUser user2;
	
	public FriendOfPK() {
		// TODO Auto-generated constructor stub
	}
	

	public FriendOfPK(RegisteredUser user1, RegisteredUser user2) {
		super();
		this.user1 = user1;
		this.user2 = user2;
	}
	

	public RegisteredUser getUser1() {
		return user1;
	}
	
	public RegisteredUser getUser2() {
		return user2;
	}

	@Override
	public boolean equals(Object o){
		try{
			if(o == null){
				return false;
			}
			FriendOfPK key = (FriendOfPK)o;
			if(user1.equals(key.user1) && user2.equals(key.user2) || user1.equals(key.user2) && user2.equals(key.user1)){
				return true;
			}
			return false;
		} catch(ClassCastException e){
			return false;
		}
	}
	
	@Override
	public int hashCode(){
		return ("" + user1.getUserId() + " " + user2.getUserId()).hashCode();
	}

}
